﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ClipPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //====================================================================================================================
        public void SetButtonImage(Button b, string ResourceName)
        {
            Image i = new Image();
            i.Source = FindResource(ResourceName) as ImageSource;
            b.Content = i;
        }
        //====================================================================================================================
        private const string TrashBinName = ".Deleted";
        private void deleteFileSafe(string pathname)
        {
            string targetPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(pathname), TrashBinName);
            try
            {
                if (!System.IO.Directory.Exists(targetPath))
                {
                    System.IO.Directory.CreateDirectory(targetPath);
                }
                string targetName = System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(pathname));
                if (System.IO.File.Exists(targetName)) System.IO.File.Delete(targetName);
                System.IO.File.Move(pathname, targetName);
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("[{0}]\n{1}", pathname, e.Message), "deleteFileSafe", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        //====================================================================================================================
        private class ItemData
        {
            public bool isInCenter;
            public int Width;

        }

        private void win1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //
            // Element, który ma być wycentrowany ma mieć w Tag wisaną '*'
            // Położenie jest regulowane szerokością zerowego elementu: Label
            //
            List<ItemData> ids = new List<ItemData>();
            //
            int freeSpaceWidth = VideoClipNameHelper.ClientAreaSize.GetClientWidth(this);
            //int freeSpaceWidth = (int)win1.ActualWidth - (2*(int)SystemParameters.ResizeFrameVerticalBorderWidth);
            int widthOfAllElements = 0;
            //
            for (int k = 0; k < toolStrip.Children.Count; k++)
            {
                var x = toolStrip.Children[k];
                Label l = x as Label;
                Button b = x as Button;
                ProgressBar p = x as ProgressBar;
                if (null != l)
                {
                    ids.Add(new ItemData() { isInCenter = ("*" == (string)l.Tag), Width = (int)l.Width });
                    widthOfAllElements += (int)l.Width;
                }
                else if (null != b)
                {
                    ids.Add(new ItemData() { isInCenter = ("*" == (string)b.Tag), Width = (int)b.Width });
                    widthOfAllElements += (int)b.Width;
                }
                else if (null != p)
                {
                    ids.Add(new ItemData() { isInCenter = ("*" == (string)p.Tag), Width = (int)p.Width });
                    widthOfAllElements += (int)p.Width;
                }
                else
                {
                    ids.Add(new ItemData() { isInCenter = false, Width = 1 });
                    widthOfAllElements += 1;
                }
            }
            //
            //
            //
            //
            //
            //
            //
            //
            //
            int widthOfCenterElement = (from x in ids where x.isInCenter select x.Width).SingleOrDefault<int>();
            //
            int widthElementsWithoutPad = 0;
            for (int i = 1; i < ids.Count; i++)
            {
                if (ids[i].isInCenter) break;
                widthElementsWithoutPad += ids[i].Width;
            }
            //
            //
            //
            if ((widthElementsWithoutPad + 2) >= freeSpaceWidth)
            {
                (toolStrip.Children[0] as Label).Width = 1;
                return;
            }
            //
            //
            int widhOfZeroElement = ((freeSpaceWidth - 2) / 2) - ((widthElementsWithoutPad + (widthOfCenterElement / 2)));
            //
            if ((widhOfZeroElement + widthElementsWithoutPad) < (freeSpaceWidth - 2))
            {
                (toolStrip.Children[0] as Label).Width = widhOfZeroElement;
            }
            else
            {
                widhOfZeroElement = (freeSpaceWidth - 1) - widthElementsWithoutPad;
                if (widhOfZeroElement < 0) widhOfZeroElement = 1;
                (toolStrip.Children[0] as Label).Width = widhOfZeroElement;
            }

        }
        //====================================================================================================================
        //====================================================================================================================
    }//class
}//namespace
